package com.learn.annotation.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AnnotationAspect {

    @Pointcut("execution(* com.learn.annotation.services.HelloWordServiceImpl.*(..))")
    public void helloWordServiceImpl(){}

    @Before("helloWordServiceImpl() && args(str)")
    public void before(String str){
        System.out.println("before: "+str);
    }

    @After("helloWordServiceImpl() && args(str)")
    public void after(String str){
        System.out.println("after: "+str);
    }

    @AfterReturning(value = "helloWordServiceImpl() && args(str)", returning = "retVal", argNames = "str,retVal")
    public void afterReturning(String str, Object retVal){
        System.out.println("afterReturning: params="+str+" return="+retVal);
    }

    @AfterThrowing(value = "helloWordServiceImpl() && args(str)", throwing = "ex")
    public void afterThrowing(String str, Exception ex){
        System.out.println("afterThrowing: param="+str+" exception="+ex.getMessage());
    }

    @Around("helloWordServiceImpl() && args(str)")
    public Object around(ProceedingJoinPoint joinPoint, String str) throws Throwable {
        Object obj = null;
        System.out.println("around before");
        obj = joinPoint.proceed();
        System.out.println("around after: "+obj);
        return obj;
    }
}
